package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.DeptmentDao;
import com.oocl.server.po.Deptment;
import com.oocl.server.po.Person;
import com.oocl.util.DbUtil;

public class DeptmentDaoImpl implements DeptmentDao {

	@Override
	public int addDept(Deptment d) {
		String sql = "insert into deptment(did,dname,city) values (avril_02.nextval,?,?)";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;// 执行成功的条数
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, d.getDname());
			pst.setString(2, d.getCity());
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int delDept(int did) {
		String sql = "delete from deptment where did=?";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setInt(1, did);
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int updateDept(Deptment d) {
		String sql = "update deptment set dname=?,city=? where did=?";
		
		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();
		
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setString(1, d.getDname());
			pst.setString(2, d.getCity());
			pst.setInt(3, d.getDid());
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public List<Deptment> listDept() {
		List<Deptment> ld = new ArrayList<Deptment>();
		String sql = "select * from deptment";
		
		Deptment d = new Deptment();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		
		int index = 0;
		try {
			pst = con.prepareStatement(sql);
			rs= pst.executeQuery();
			while (rs.next()) {
				d.setDid(rs.getInt("did"));
				d.setDname(rs.getString("dname"));
				d.setCity(rs.getString("city"));
				ld.add(d);
//				System.out.println(ld.get(index) + "........");
//				index++;
			}
//			for (int i = 0; i < ld.size(); i++) {
//				System.out.println(ld.get(i) + "........");
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DbUtil.free(con, pst, rs);
		}
		
		return ld;
	}

}
